package com.panshi.controller;
import com.panshi.AnswerApi;
import com.panshi.IssApi;
import com.panshi.api.UserPoth;
import com.panshi.api.api.AnswerCommentsHDPort;
import com.panshi.api.api.TypePort;
import com.panshi.api.bean.AnswerCommentsHDBean;
import com.panshi.api.util.PageSizeVO;
import com.panshi.bean.*;
import com.panshi.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class AnswersController {

    @Autowired
    private CommonUtil commonUtil;

    @Autowired
    private IssApi issApi;

    @Autowired
    private TypePort typePort;

    @Autowired
    private AnswerCommentsHDPort answerCommentsHDPort;


    @Autowired
    private AnswerApi answerApi;

    @Autowired
    private UserPoth userPoth;

    @Autowired
    private ResponseBean responseBean;

    /**
     * 主页
     * @param model
     * @return
     */
    @RequestMapping("/answers/main")
    public String answersMain(HttpServletRequest request,Model model,String list,Integer toothType) throws Exception {
        model.addAttribute("wd","wd");
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        if(auto_login.length()>0){
            UserVO userQuery = userPoth.findUserQuery(Integer.valueOf(auto_login));//查询用户
            model.addAttribute("user",userQuery);
        }
        model.addAttribute("auto",auto_login);

        PageModel pageModel=new PageModel();
        pageModel.setPageSize(10);
        pageModel.setStartIndex(1);
        if(toothType!=null){
            pageModel.setTid(toothType);
        }


        List<IssBean> allIss = null;
        if( list==null){//推荐

            allIss=issApi.findAllIss(pageModel);
        }else if("12".equals(list)){//已回答
            pageModel.setAnswer(1);
            allIss=issApi.findAllIss(pageModel);

        }else if("13".equals(list)){//未回答
            pageModel.setAnswer(0);
            allIss=issApi.findAllIss(pageModel);
        }
        Collections.shuffle(allIss);//打乱集合

        if(toothType == null){
            model.addAttribute("daList",issApi.findHostIss(0,0));//大家推荐
        }else{
            model.addAttribute("daList",issApi.findHostIss(toothType,0));//大家推荐
        }

        model.addAttribute("answersType",list);//热门，推荐类型
        model.addAttribute("toothType",toothType);//牙齿类型
        model.addAttribute("alliss",allIss);

        model.addAttribute("typeList",typePort.queryAnswerInfoAll());
        typePort.queryAnswerInfoAll().forEach(var -> System.out.println(var));
        return "answers/answers_main";
    }


    /**
     * 个人详情
     */
    @GetMapping("/answers/{id}")
    public String answersParticulars(@PathVariable String id,Model model,HttpServletRequest request){
        IssBean issBean=issApi.getByQid(Integer.valueOf(id));

        List<AnswerBean> byQidAnswer = issApi.getByQidAnswer(issBean.getQid());
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        if(auto_login.length()>0){
            UserVO userQuery = userPoth.findUserQuery(Integer.valueOf(auto_login));//查询用户
            model.addAttribute("user",userQuery);
        }
        model.addAttribute("xgList",issApi.aboutQa(issBean.getTid()));//相关问题
        model.addAttribute("djList",issApi.findHostIss(0,1));//大家都在问

        model.addAttribute("answer",issBean);
        model.addAttribute("typeList",typePort.queryAnswerInfoAll());//牙齿类型
        model.addAttribute("toothType",issBean.getTid());
        model.addAttribute("byanswer",byQidAnswer);
        model.addAttribute("auto",auto_login);
        return "answers/answers_particulars";
    }


    @PostMapping("/answers/addReply")
    @ResponseBody
    public int add(AnswerNoBean answerNoBean){
        int i = answerApi.initAnswer(answerNoBean);
        return i;
    }

    @PostMapping("/answers/append")
    @ResponseBody
    public ModelAndView answersAppend(String type, Integer current, Integer cateId, ModelAndView modelAndView) throws Exception {
        PageModel pageModel=new PageModel();
        pageModel.setPageSize(10);
        pageModel.setStartIndex(current);

        if(cateId!=null){
            pageModel.setTid(cateId);
        }
        List<IssBean> allIss = null;
        if( type==null || type==""){//推荐
            allIss=issApi.findAllIss(pageModel);
        }else if("12".equals(type)){//已回答
            pageModel.setAnswer(1);
            allIss=issApi.findAllIss(pageModel);
        }else if("13".equals(type)){//未回答
            pageModel.setAnswer(0);
            allIss=issApi.findAllIss(pageModel);
        }
        modelAndView.addObject("answerList",allIss);
        modelAndView.setViewName("answers/answers_page");
        return modelAndView;
    }


    @PostMapping("/answers/commentlist")
    @ResponseBody
    public Object queryPl(PageSizeVO pageSizeVO, HttpServletRequest request,ModelAndView modelAndView){

        pageSizeVO.setPageSize(5);
        List<AnswerCommentsHDBean> answerCommentsHDBeans ;

        if(pageSizeVO.getAid()!=0){
            answerCommentsHDBeans= answerCommentsHDPort.queryAssignAnswerCommentsAll(pageSizeVO);
            modelAndView.addObject("pid",pageSizeVO.getParentid());
            System.out.println("1");
        }else{
            answerCommentsHDBeans=answerCommentsHDPort.queryAssginAnswerReplyInfo(pageSizeVO);
            modelAndView.addObject("pid",1);
            System.out.println("2");
        }

        if(answerCommentsHDBeans.size()==0){
            return "";
        }


        modelAndView.addObject("commentList",answerCommentsHDBeans);
        answerCommentsHDBeans.forEach(var -> System.out.println(var));

        modelAndView.setViewName("/answers/answers_comment_join");
        return modelAndView;
    }

    @PostMapping("/answers/addComment")
    @ResponseBody
    public ModelAndView addPl(AnswerCommentsHDBean answerCommentsHDBean, HttpServletRequest request,ModelAndView modelAndView){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        answerCommentsHDBean.setUid(Integer.valueOf(auto_login));


        AnswerCommentsHDBean answerCommentsHDBean1 = answerCommentsHDPort.addAnswerCommentsHDInfo(answerCommentsHDBean);

        if(answerCommentsHDBean.getParentid()==0){
            modelAndView.addObject("oneReViews",answerCommentsHDBean1);
        }else{
            if((answerCommentsHDBean1.getParentid()+"").equals(answerCommentsHDBean.getDepth())){
                modelAndView.addObject("twoReViews",answerCommentsHDBean1);
            }else{
                modelAndView.addObject("threeReViews",answerCommentsHDBean1);
            }
        }

        modelAndView.setViewName("answers/answers_comment_join");
        return modelAndView;
    }

    @PostMapping("/answers/dianzan")
    @ResponseBody
    public Map dianzan(int qaReplyId, int type, HttpServletRequest request){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        String i = answerApi.clientLike(qaReplyId, Integer.valueOf(auto_login), type);



        Map map=new HashMap();

        if(type==0){//点赞
            map.put("type",0);
        }else{
            map.put("type",1);
        }

            map.put("success","点赞成功");

            map.put("data",i);
            map.put("code",200);

        return map;
    }

    @PostMapping("/answers/add")
    @ResponseBody
    public String answersAdd(IssBean issBean,HttpServletRequest request){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        issBean.setUid(Integer.valueOf(auto_login));

        String str=issApi.initIss(issBean);
        return str;
    }

    @PutMapping("/answers/update")
    @ResponseBody
    public ResponseBean answerUpdate(AnswerBean2 answerBean2){
        int i = answerApi.updateAnswer(answerBean2);

        responseBean.setCode("200");
        responseBean.setMsg("修改成功！待审核");
        return responseBean;
    }

    @PostMapping("/answers/delete")
    @ResponseBody
    public ResponseBean answerDelete(AnswerBean2 answerBean2){
        answerApi.deleteAnswer(answerBean2);
        responseBean.setCode("200");
        responseBean.setMsg("删除成功！");
        return responseBean;
    }
    @PostMapping("/qa/del")
    @ResponseBody
    public ResponseBean qaDelete(int qid){
        issApi.deleteIss(qid,1);
        responseBean.setCode("200");
        responseBean.setMsg("删除成功！");
        return responseBean;
    }


}
